package com.hotbitmapgg.moequest.module.commonality;

import butterknife.Bind;
import butterknife.ButterKnife;
import com.hotbitmapgg.moequest.R;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

/**
 * Created by hcc on 16/6/30 21:57
 * 100332338@qq.com
 * <p/>
 * App欢迎页面
 */
public class AppSplashActivity extends Activity {

  @Bind(R.id.splash_image)
  ImageView mSplashImage;

  private static final int ANIMATION_TIME = 2000;

  private static final float SCALE_END = 1.13F;

  private Subscription subscribe;


  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    ButterKnife.bind(this);
    subscribe = Observable.timer(1000, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(aLong -> {
          startAnim();
        });
  }


  private void startAnim() {

    ObjectAnimator animatorX = ObjectAnimator.ofFloat(mSplashImage, "scaleX", 1f, SCALE_END);
    ObjectAnimator animatorY = ObjectAnimator.ofFloat(mSplashImage, "scaleY", 1f, SCALE_END);

    AnimatorSet set = new AnimatorSet();
    set.setDuration(ANIMATION_TIME).play(animatorX).with(animatorY);
    set.start();

    set.addListener(new AnimatorListenerAdapter() {

      @Override
      public void onAnimationEnd(Animator animation) {

        startActivity(new Intent(AppSplashActivity.this, MainActivity.class));
        AppSplashActivity.this.finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
      }
    });
  }


  @Override
  public void onBackPressed() {

    super.onBackPressed();
    if (subscribe != null && !subscribe.isUnsubscribed()) {
      subscribe.unsubscribe();
    }
  }
}
